package com.ruoyi.ctcx.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ctcx.domain.TbInsuranceAttachment;
import com.ruoyi.ctcx.domain.ro.InsuranceParam;
import com.ruoyi.ctcx.domain.ro.RiskParam;
import com.ruoyi.ctcx.domain.vo.TbInsuranceRiskAmountVo;
import com.ruoyi.ctcx.domain.vo.TbInsuranceRiskContracStaticstVo;
import com.ruoyi.ctcx.domain.vo.TbInsuranceRiskContractVo;
import com.ruoyi.ctcx.service.ITbInsuranceAttachmentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ctcx.domain.TbInsuranceRiskContract;
import com.ruoyi.ctcx.service.ITbInsuranceRiskContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 保险风控合同Controller
 * 
 * @author ruoyi
 * @date 2024-11-19
 */
@RestController
@RequestMapping("/risk/contract")
public class TbInsuranceRiskContractController extends BaseControllerExt
{
    @Autowired
    private ITbInsuranceRiskContractService tbInsuranceRiskContractService;

    @Autowired
    private ITbInsuranceAttachmentService tbInsuranceAttachmentService;

    /**
     * 查询保险风控合同列表
     */
    @PreAuthorize("@ss.hasPermi('risk:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsuranceParam insuranceParam)
    {
        initDeptId(insuranceParam);
        startPage();
        List<TbInsuranceRiskContractVo> list = tbInsuranceRiskContractService.getRiskContractListVo(insuranceParam);
        return getDataTable(list);
    }


    /**
     * 查询保险风控合同统计
     */
    @PreAuthorize("@ss.hasPermi('risk:contract:statics')")
    @GetMapping("/statics")
    public TableDataInfo statics(RiskParam riskParam)
    {
        startPage();
        List<TbInsuranceRiskContracStaticstVo> list = tbInsuranceRiskContractService.getRiskContractStaticsVo(riskParam);
        return getDataTable(list);
    }

    /**
     * 查询保险风控费用合计
     */
    @PreAuthorize("@ss.hasPermi('risk:contract:statics')")
    @GetMapping("/staticsRiskAmount")
    public AjaxResult staticsRiskAmount(RiskParam riskParam)
    {
        TbInsuranceRiskAmountVo amountVo = tbInsuranceRiskContractService.getRiskAmount(riskParam);
        return success(amountVo);
    }


    /**
     * 导出保险风控合同统计
     */
    @PreAuthorize("@ss.hasPermi('risk:contract:statics')")
    @Log(title = "风控费用统计", businessType = BusinessType.EXPORT)
    @PostMapping("/exportStatics")
    public void exportStatics(HttpServletResponse response, RiskParam riskParam)
    {
        List<TbInsuranceRiskContracStaticstVo> list = tbInsuranceRiskContractService.getRiskContractStaticsVo(riskParam);
        ExcelUtil<TbInsuranceRiskContracStaticstVo> util = new ExcelUtil<TbInsuranceRiskContracStaticstVo>(TbInsuranceRiskContracStaticstVo.class);
        util.exportExcel(response, list, "风控费用统计");
    }


    /**
     * 导出保险风控合同列表
     */
    @PreAuthorize("@ss.hasPermi('risk:contract:export')")
    @Log(title = "保险风控合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InsuranceParam insuranceParam)
    {
        initDeptId(insuranceParam);
        List<TbInsuranceRiskContractVo> list = tbInsuranceRiskContractService.getRiskContractListVo(insuranceParam);
        ExcelUtil<TbInsuranceRiskContractVo> util = new ExcelUtil<TbInsuranceRiskContractVo>(TbInsuranceRiskContractVo.class);
        util.exportExcel(response, list, "保险风控合同数据");
    }

    /**
     * 获取保险风控合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('risk:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        TbInsuranceRiskContract tbInsuranceRiskContract = tbInsuranceRiskContractService.selectTbInsuranceRiskContractById(id);

        List<TbInsuranceAttachment> tbInsuranceAttachmentList = tbInsuranceAttachmentService.selectTbInsuranceAttachmentByMainId(id);

        if (null != tbInsuranceAttachmentList && !tbInsuranceAttachmentList.isEmpty())
        {
            tbInsuranceRiskContract.setTbInsuranceAttachmentList(tbInsuranceAttachmentList);
        }
        return success(tbInsuranceRiskContract);
    }

    /**
     * 新增保险风控合同
     */
    @PreAuthorize("@ss.hasPermi('risk:contract:add')")
    @Log(title = "保险风控合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbInsuranceRiskContract tbInsuranceRiskContract)
    {
        //保存主表信息
        tbInsuranceRiskContract.setCreateBy(getUsername());
        tbInsuranceRiskContractService.insertTbInsuranceRiskContract(tbInsuranceRiskContract);

        //保存附件信息
        tbInsuranceAttachmentService.saveAttachment(tbInsuranceRiskContract.getId().toString(),tbInsuranceRiskContract.getTbInsuranceAttachmentList(),getUsername());
        return toAjax(1);
    }

    /**
     * 修改保险风控合同
     */
    @PreAuthorize("@ss.hasPermi('risk:contract:edit')")
    @Log(title = "保险风控合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbInsuranceRiskContract tbInsuranceRiskContract)
    {
        //保存主表信息
        tbInsuranceRiskContract.setUpdateBy(getUsername());
        tbInsuranceRiskContractService.updateTbInsuranceRiskContract(tbInsuranceRiskContract);

        //保存附件信息
        tbInsuranceAttachmentService.saveAttachment(tbInsuranceRiskContract.getId().toString(),tbInsuranceRiskContract.getTbInsuranceAttachmentList(),getUsername());
        return toAjax(1);
    }

    /**
     * 删除保险风控合同
     */
    @PreAuthorize("@ss.hasPermi('risk:contract:remove')")
    @Log(title = "保险风控合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbInsuranceRiskContractService.deleteTbInsuranceRiskContractByIds(ids));
    }
}
